package com.sdy.healthcode.biz.service.impl;

import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.IdCardUtil;
import com.sdy.common.utils.StringUtil;
import com.sdy.healthcode.biz.dto.userpatient.UserPatientDTO;
import com.sdy.healthcode.biz.model.User;
import com.sdy.healthcode.biz.model.UserPatient;
import com.sdy.healthcode.biz.mapper.UserPatientMapper;
import com.sdy.healthcode.biz.service.UserPatientService;
import com.sdy.healthcode.biz.service.UserService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户-就诊人关联表 服务实现类
 * </p>
 *
 * @author zzq
 * @since 2020-03-31
 */
@Slf4j
@Service
public class UserPatientServiceImpl extends BaseServiceImpl<UserPatient> implements UserPatientService {
    @Autowired
    private UserPatientMapper userPatientMapper;
    @Autowired
    private UserService userService;

    @Override
    public void addUserPatient(Integer userId, UserPatientDTO userPatientDTO) throws BizException {
        if (userPatientDTO.getPhone() != null) {
            userPatientDTO.setPhone(userPatientDTO.getPhone().trim());
        }
        if (userPatientDTO.getCardId() != null) {
            userPatientDTO.setCardId(userPatientDTO.getCardId().trim());
        }
        Assert.isNull(userId, "参数错误");
        Assert.isBlank(userPatientDTO.getName(), "姓名不能为空");
        Assert.isTrue(userPatientDTO.getCardId() == null || !IdCardUtil.IDCardValidate(userPatientDTO.getCardId()).equals("true"), "身份证格式错误");
        Assert.isTrue(StringUtil.isNoneEmpty(userPatientDTO.getPhone()) && !userPatientDTO.getPhone().matches(com.sdy.common.constant.Constants.REGEX_PHONE), "手机号格式错误");
        User user = userService.getById(userId);
        int existCnt = lambdaQuery()
                .eq(UserPatient::getUserId, userId)
                .eq(UserPatient::getCardId, userPatientDTO.getCardId())
                .eq(UserPatient::getDeleted, 0)
                .count();
        Assert.isTrue(existCnt > 0 || userPatientDTO.getCardId().equals(user.getCardId()), "身份证号已被使用");
        Date now = new Date();
        save(new UserPatient()
                .setUserId(userId)
                .setCardId(userPatientDTO.getCardId())
                .setPhone(userPatientDTO.getPhone())
                .setName(userPatientDTO.getName())
                .setDeleted(0)
                .setCreateTime(now)
                .setUpdateTime(now));
    }

    @Override
    public void updateUserPatient(Integer userId, UserPatientDTO userPatientDTO) throws BizException {
        if (userPatientDTO.getPhone() != null) {
            userPatientDTO.setPhone(userPatientDTO.getPhone().trim());
        }
        if (userPatientDTO.getCardId() != null) {
            userPatientDTO.setCardId(userPatientDTO.getCardId().trim());
        }
        Assert.isNull(userId, "参数错误");
        Assert.isBlank(userPatientDTO.getName(), "姓名不能为空");
        Assert.isNull(userPatientDTO.getUserPatientId(), "参数错误");
        Assert.isTrue(userPatientDTO.getCardId() == null || !IdCardUtil.IDCardValidate(userPatientDTO.getCardId()).equals("true"), "身份证格式错误");
        Assert.isTrue(StringUtil.isNoneEmpty(userPatientDTO.getPhone()) && !userPatientDTO.getPhone().matches(com.sdy.common.constant.Constants.REGEX_PHONE), "手机号格式错误");
        User user = userService.getById(userId);
        int existCnt = lambdaQuery()
                .eq(UserPatient::getUserId, userId)
                .eq(UserPatient::getCardId, userPatientDTO.getCardId())
                .ne(UserPatient::getId, userPatientDTO.getUserPatientId())
                .eq(UserPatient::getDeleted, 0)
                .count();
        Assert.isTrue(existCnt > 0 || userPatientDTO.getCardId().equals(user.getCardId()), "身份证号已被使用");
        UserPatient patient = getById(userPatientDTO.getUserPatientId());
        Assert.isNull(patient, "参数错误");
        Assert.isTrue(!patient.getUserId().equals(userId), "没有修改权限");
        lambdaUpdate()
                .eq(UserPatient::getId, userPatientDTO.getUserPatientId())
                .set(UserPatient::getCardId, userPatientDTO.getCardId())
                .set(UserPatient::getPhone, userPatientDTO.getPhone())
                .set(UserPatient::getName, userPatientDTO.getName())
                .update();
    }

    @Override
    public void removeUserPatient(Integer userId, Integer userPatientId) throws BizException {
        Assert.isNull(userId, "参数错误");
        Assert.isNull(userPatientId, "参数错误");
        UserPatient patient = getById(userPatientId);
        Assert.isNull(patient, "参数错误");
        Assert.isTrue(!patient.getUserId().equals(userId), "没有删除权限");
        lambdaUpdate().eq(UserPatient::getId, userPatientId).set(UserPatient::getDeleted, 1).update();
    }

    @Override
    public List<UserPatientDTO> listByUserId(Integer userId) {
        return lambdaQuery()
                .eq(UserPatient::getUserId, userId)
                .eq(UserPatient::getDeleted, 0)
                .list()
                .stream()
                .map(UserPatientDTO::fromUserPatient)
                .collect(Collectors.toList());
    }
}
